package task

import (
	"cvevulner/common"
	"cvevulner/taskhandler"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
	"os"
)

func ProcHookEvent() error {
	defer common.Catchs()
	logs.Info("Remove duplicate webhook event task start")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("config init error:", err)
		return err
	}
	hookurl := BConfig.String("hook::hookurl")
	owner := BConfig.String("gitee::owner")
	accessToken := os.Getenv("GITEE_TOKEN")
	gaussOwner := beego.AppConfig.String("opengauss::gauss_owner")
	gitGaussToken := beego.AppConfig.String("opengauss::git_gauss_token")
	pwd := BConfig.String("hook::hookpwd")
	// Get the data source of the table
	err = taskhandler.ProcHookEvent(hookurl, owner, accessToken, pwd, gaussOwner, gitGaussToken)
	logs.Info("Remove duplicate webhook event task ends")
	return err
}

// Create webhooks for all repo
func CreateWebhookEvent() error {
	defer common.Catchs()
	logs.Info("Create webhook event start")
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("config init error:", err)
		return err
	}
	hookurl := BConfig.String("hook::hookurl")
	owner := BConfig.String("gitee::owner")
	accessToken := os.Getenv("GITEE_TOKEN")
	gaussOwner := beego.AppConfig.String("opengauss::gauss_owner")
	gitGaussToken := beego.AppConfig.String("opengauss::git_gauss_token")
	pwd := BConfig.String("hook::hookpwd")
	// Get the data source of the table
	err = taskhandler.CreateHookEvent(hookurl, owner, accessToken, pwd, gaussOwner, gitGaussToken)
	logs.Info("End of creating webhook event")
	return err
}
